﻿using System.IO;
using UnityEngine;
using System;
using System.Collections;
using Inlycat.Log;

namespace Inlycat
{
    public class FileHelper
    {
        /// <summary>
        /// 加载文件(byte[])
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="onloadSucc">加载成功回调</param>
        /// <param name="onloadError">加载失败回调</param>
        /// <returns></returns>
        public static void LoadFile(string path, Action<byte[]> onloadSucc, Action<string> onloadError)
        {
            if (onloadSucc == null)
            {
                onloadSucc = t => { };
            }
            if (onloadError == null)
            {
                onloadError = Debug.Log;
            }

            // 安卓平台下的streamingAssets目录只能用www读取
            if (path.Contains(Application.streamingAssetsPath))
            {
#if UNITY_ANDROID
            CoroutineManager.Instance.DoAction(loadBytes(path, onloadSucc, onloadError));
#else
                try
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    onloadSucc(bytes);
                }
                catch (Exception e)
                {
                    onloadError(e.Message);
                }
#endif
            }
            else
            {
                try
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    onloadSucc(bytes);
                }
                catch (Exception e)
                {
                    onloadError(e.Message);
                }
            }
        }

        private static IEnumerator loadBytes(string path, Action<byte[]> onloadSucc, Action<string> onloadError)
        {
            WWW www = new WWW(path);
            yield return www;
            if (string.IsNullOrEmpty(www.error))
            {
                onloadSucc(www.bytes);
            }
            else
            {
                onloadError(www.error);
            }
            www.Dispose();
            yield return null;
        }

        /// <summary>
        /// 加载文件(string)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="onloadSucc">加载成功回调</param>
        /// <param name="onloadError">加载失败回调</param>
        /// <returns></returns>
        public static void LoadFile(string path, Action<string> onloadSucc, Action<string> onloadError)
        {
            if (onloadSucc == null)
            {
                onloadSucc = t => { };
            }
            if (onloadError == null)
            {
                onloadError = Debug.Log;
            }

            // 安卓平台下的streamingAssets目录只能用www读取
            if (path.Contains(Application.streamingAssetsPath))
            {
#if UNITY_ANDROID
            CoroutineManager.Instance.DoAction(loadText(path, onloadSucc, onloadError));
#else
                try
                {
                    string text = File.ReadAllText(path);
                    onloadSucc(text);
                }
                catch (Exception e)
                {
                    onloadError(e.Message);
                }
#endif
            }
            else
            {
                try
                {
                    string text = File.ReadAllText(path);
                    onloadSucc(text);
                }
                catch (Exception e)
                {
                    onloadError(e.Message);
                }
            }
        }

        private static IEnumerator loadText(string path, Action<string> onloadSucc, Action<string> onloadError)
        {
            WWW www = new WWW(path);
            yield return www;
            if (string.IsNullOrEmpty(www.error))
            {
                onloadSucc(www.text);
            }
            else
            {
                onloadError(www.error);
            }
            www.Dispose();
            yield return null;
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="info">写入内容</param>
        /// <param name="append">是否追加</param>
        public static void WriteFile(string path, string info, bool append)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, append);
                sw.Write(info);
                sw.Close();
                sw.Dispose();
                LogManager.Instance.Log("Write Suc : " + path, "FileHelper", "WriteFile");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// write to a file with the given bytes
        /// </summary>
        /// <param name="path">the file path</param>
        /// <param name="content">the file content bytes</param>
        public static void WriteFile(string path, byte[] content)
        {
            try
            {
                File.WriteAllBytes(path, content);
                LogManager.Instance.Log("Write Suc : " + path, "FileHelper", "WriteFile");
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 清空文件夹
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        public static void ClearDirectory(string dirPath)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(dirPath);
                foreach (DirectoryInfo d in di.GetDirectories())
                {
                    d.Delete(true);
                }
                foreach (FileInfo f in di.GetFiles())
                {
                    f.Delete();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 获取文件夹下的所有文件名
        /// </summary>
        /// <param name="dirPath">文件夹路径</param>
        /// <returns></returns>
        public static string[] GetDirectoryFiles(string dirPath)
        {
            try
            {
                return Directory.GetFiles(dirPath);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}